﻿using System;
using System.Web;
using System.Web.Caching;

namespace Ajax.Common
{
    /*
     * HttpContext.Cache,HttpRuntime.Cache,用法上没有区别，就是使用范围不同．　　 
     * HttpContext.Cache是基于上下文，对同一个用户起做用，如果换了一个访问，那么这个ＣＡＣＨＥ就不起做用了． 
     * HttpRuntime.Cache是全局的，只要有内容，任何用户都可以访问
     * HttpContext.Current.Cache 只可能用于 Web 应用的缓存
     * HttpRuntime.Cache 可用于非 Web 应用的缓存
     */
    /// <summary>
    /// 缓存的字符串－－在数据库访问基类没有调用
    /// </summary>
    public class MyCache
    {
        /// <summary>
        /// 读取缓存 
        /// </summary>
        /// <param name="strID"></param>
        /// <returns></returns>
        static public object ReadCache(string CacheID)
        {
            if (HttpRuntime.Cache[CacheID] != null)
            {
                return HttpRuntime.Cache[CacheID];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 读取缓存返回string类型
        /// </summary>
        /// <param name="strID"></param>
        /// <returns></returns>
        static public string ReadCacheString(string CacheID)
        {
            string Tempstr;
            if (HttpRuntime.Cache[CacheID] != null)
            {
                return Tempstr = HttpRuntime.Cache[CacheID] as string;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="strID"></param>
        /// <param name="InPut"></param>
        /// <param name="CacheSeconds"></param>
        static public void InsertCache(string CacheID, object InPut, double CacheSeconds)
        {
            if (InPut == null)
            {
                return;
            }

            HttpRuntime.Cache.Insert(CacheID, InPut, null, DateTime.Now.AddSeconds(CacheSeconds), Cache.NoSlidingExpiration);
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="strID"></param>
        static public void RemoveCache(string ID)
        {
            HttpRuntime.Cache.Remove(ID);
        }
    }
}